
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_module/service/serviee_method.dart';
import 'package:html/parser.dart' show parse;

class ZongHeQueryModel with ChangeNotifier {
  int num=1;
  int page=1;
  bool query=false;
  Map map;
  String url;
  List<List> data=[];
  List itemList=[];
  EasyRefreshController controller = EasyRefreshController();
  Future onLoad() async {
    await getHttpData(url,data:map).then((value){
      controller.finishLoad(success: true,noMore:page>=num);
    });
  }
  Future getHttpData(String url,{Map data}) async{

    Map<String,String> map=Map();

    map["pageIndex"]=page.toString();
    //map["pageSize"]="1";
    map["companyId"]="402880185889ef6e015889f8c51e0000";
    map["userId"]="8a2a2a295c1ec767015c2ddd9d1e0001";
    this.url = url;
    this.map = data;

    if(data!=null){
      if(query){
      }else{
        query=!query;
        page=1;
        this.data.clear();
      }

      data["pageIndex"]=page.toString();
      data["companyId"]="402880185889ef6e015889f8c51e0000";
      data["userId"]="8a2a2a295c1ec767015c2ddd9d1e0001";
    }
    await getHttpPost(url, data??map).then((value){
      this.page++;
      getListData(value);
      notifyListeners();
    });

  }


  void getListData(value) {
    var document = parse(value);
    var body= document.getElementsByTagName("tr");
//    print("tr========"+body);
    var span= document.getElementsByTagName("span");
    for(int i=0;i<span.length;i++){
      String name= span[2].outerHtml.substring(25,span[2].outerHtml.length-7).trim();
      if(i==2){
        num=int.parse(name);
      }

    }
    for(int i=1;i<body.length;i++){
      var trim = body[i].outerHtml.substring(14,46).trim();
      var list=body[i].getElementsByTagName("td");
      List item=[];
//      item.add(trim);
      for(int j=1;j<list.length;j++){
        String name = "";
        if(j < 6) {
          name = list[j].outerHtml.substring(
              4, list[j].outerHtml.length - 5).trim();
        } else {
          var list1 =list[j].getElementsByTagName("a");
          List<String> ss = list1[0].outerHtml.trim().split(">");
          if(ss.length >= 2){
            List<String> shu = ss[1].split("<");
            if(shu.length == 2){
              name = shu[0];
            }
          }

//          name = list[j].outerHtml.substring(
//              list[j].outerHtml.length - 9, list[j].outerHtml.length - 9).trim();
        }
        item.add(name);
      }
      data.add(item);
    }

  }
}